import requests
import json
from loaddata.mgDB import PailieDatabase
import urllib3
from tqdm import tqdm
urllib3.disable_warnings()
'''
下载排列三数据
'''
class LoadDataPailie:
    def __init__(self):
        self.mgdb = PailieDatabase()
    '''
    爬取数据
    '''
    def gethtml(self,url):
        headers = {
            "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
        }
        res = requests.get(url, verify=False,headers=headers)
        res.encoding = "utf-8"
        return res.text

    # 索取数据
    def getPailieList(self,page):
        url = "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=35&provinceId=0&pageSize=30&isVerify=1&pageNo={}" .format(
            page)
        data = self.gethtml(url)
        res = json.loads(data)
        return res.get("value").get("list")

    # 获取数据
    def loadData(self):
        url = "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=35&provinceId=0&pageSize=30&isVerify=1&pageNo=1"
        # 获取总页数 ，
        pagedata = self.gethtml(url)
        result = json.loads(pagedata)
        pages = result.get("value").get("pages")  # 总页数
        for p in tqdm(range(pages,0,-1)):
            ds = self.getPailieList(p)
            self.save(ds)

    # 跟新获取数据
    def uploadData(self):
        url = "https://webapi.sporttery.cn/gateway/lottery/getHistoryPageListV1.qry?gameNo=35&provinceId=0&pageSize=30&isVerify=1&pageNo=1"
        # 获取总页数 ，
        pagedata = self.gethtml(url)
        result = json.loads(pagedata)
        pages = result.get("value").get("pages")  # 总页数
        for p in range(1,pages+1):
            ds = self.getPailieList(p)
            r = self.update(ds)  # 返回空，说明要需要下一页看看，否则结束跟新，如果有新数据，就保存，如果都是旧数据，说明这一页已经跟新了
            if len(r):
                break



    # 保持到数据库
    def save(self,ds):
        self.mgdb.batch_sava_to_pailie(ds)

    # 保持到数据库
    def update(self,ds):
        return self.mgdb.isexit(ds)


if __name__ == '__main__':
    for t in ['wudi']:
        LoadDataPailie().loadData()

    # 正在下载
    # 2023 - 07 - 22
    # 00: 00:00
    # 到
    # 2023 - 0
    # 8 - 21
    # 00: 00:00
    # 的数据